package abstraction;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import utils.Parameters;

/**
 * A specialized JButton design for a toolBar
 * Have an active and an inactive state
 * @author KoinKoin Team
 *
 */
public class ToolBarButton extends JButton
{
    private boolean m_isActive;
    private ImageIcon m_activeImg;
    private ImageIcon m_inactiveImg;
    
    /**
     * Active/inactive Button constructor
     * 
     * @param activeURL : path to active image
     * @param inactiveURL : path to inactive image
     */
    public ToolBarButton(String activeURL, String inactiveURL){
        super();
        
        m_isActive = false;
        m_activeImg = new ImageIcon(this.getClass()
                .getResource("/images/"+activeURL));
        m_inactiveImg = new ImageIcon(this.getClass()
                .getResource("/images/"+inactiveURL));
        
        updateImg();
    }
    
    /**
     * Only one state button
     * @param activeURL
     */
    public ToolBarButton(String activeURL){
        super();
        
        m_isActive = false;
        m_activeImg = new ImageIcon(this.getClass()
                .getResource("/images/"+activeURL));
        m_inactiveImg = new ImageIcon(this.getClass()
                .getResource("/images/"+activeURL));
        
        updateImg();
    }
    
    public ToolBarButton(Color color)
    {
        super();
        
        m_isActive = false;
        int size = Parameters.SIZE_COLOR_BUTTON;
        
        BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
        Graphics g = img.getGraphics();
        g.setColor(color);
        g.fillRect(0, 0, size, size);
        g.dispose();
        
        m_activeImg = new ImageIcon(img);
        m_inactiveImg = new ImageIcon(img);
        
        updateImg();
    }

    /**
     * update the icon depends on state.
     */
    public void updateImg(){
        if (m_isActive)
            this.setIcon(m_activeImg);
        else
            this.setIcon(m_inactiveImg);
    }
    
    public void setInactive(){
        m_isActive = false;
        updateImg();
    }
    
    public void setActive(){
        m_isActive = true;
        updateImg();
    }
}
